#include <asm/sbidef.h>
#include <asm/sbiasm.h>

// #define BOOT_LOADER_SIG_OFFSET 0x1fe
// #define OS_SIZE_LOC (BOOT_LOADER_SIG_OFFSET - 2)
// #define OS_SIZE_LOC 0x1fc

.equ os_size_loc, 0x502001fc

// 1. kernel address (move kernel to here ~)
.equ kernel, 0x50200000//这里修改了kernel的对应地址

// 2. kernel main address (jmp here to start kernel main!)
.equ kernel_main, 0x50201000

.text
.global main

main:
	// fence on all memory and I/O
	fence

	// print a \n to avoid system output error...
	li a0, 10
	SBI_CALL SBI_CONSOLE_PUTCHAR

	// 1) task1 call BIOS print string "It's bootblock!"
	la a0, msg
	SBI_CALL SBI_CONSOLE_PUTSTR

	// 2) task2 call BIOS read kernel in SD card and jump to kernel start
	la t1, os_size_loc//get an adress that stroing the length of kernel which is computed in creatimage
	lh t1, 0(t1)//make t1 into a number
	//为了做C-score，得把bootblock给移走,这里从sd卡重新读一份bootblock
	la a0, 0x50201000
	add a0, a0, t1
	li a1, 1
	li a2, 0
	SBI_CALL SBI_SD_READ
	add t0, a0, t1 
	addi t0, t0, 70
	jr t0 //额外添加的C-score部分结束
	la a0, kernel
	add a1, zero, t1
	addi a2, zero, 1
	SBI_CALL SBI_SD_READ
	j kernel
// while(1) --> stop here
stop:
	j stop
	nop

.data

msg: .string "It's a bootloader...\n\r"

